package net.borderwars.simulator;

import javax.swing.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * User: Eric
 * Date: Nov 21, 2004
 * Time: 9:56:07 PM
 */
public class Clock {
    private AtomicLong currentTime = new AtomicLong (0L);
    private JLabel displayLabel;

    public long getCurrentTime () {
        return (currentTime.longValue ());
    }

    public long advance (long l) {
        checkParameter (l);
        long rtn = currentTime.addAndGet (l);
        updateLabel ();
        return (rtn);
    }

    private void updateLabel () {
        if (displayLabel != null) {
            displayLabel.setText ("Time: " + currentTime.get ());
        }
    }

    private void checkParameter (long l) {
        if (currentTime.longValue () > l) {
            throw new UnsupportedOperationException ("Clocks can't go backwards!");
        }
    }

    public void set (long l) {
        checkParameter (l);
        currentTime.set (l);
        updateLabel ();
    }

    public void setDisplayLabel (JLabel label) {
        displayLabel = label;
    }

}
